(function (window, $, Camera) {

    $(function () {
        // 设置iframe的高度，非精准适应屏幕高度
        var docHeight = $(document).height();
        $('#reportContainer').height(docHeight - 300);

        var cardModel = [
            {type: "1", name: "身份证"},
            {type: "2", name: "户口簿"},
            {type: "3", name: "护照"},
            {type: "4", name: "军官证"},
            {type: "5", name: "士兵证"},
            {type: "6", name: "港澳居民来往内地通行证"},
            {type: "7", name: "台湾同胞来往内地通行证"},
            {type: "8", name: "临时身份证"},
            {type: "9", name: "外国人居住证"},
            {type: "10", name: "警官证"},
            {type: "11", name: "香港身份证"},
            {type: "12", name: "澳门身份证"},
            {type: "13", name: "台湾身份证"},
            {type: "14", name: "其他证件"}
        ];
        var typesModel = [{type: 1, name: "个人简单版"},
            {type: 2, name: "个人明细版"},
            {type: 3, name: "股指期货版"}];

        var vm = avalon.define({
            $id: 'aprintInquiryPage',
            cardModel: cardModel,
            typesModel: typesModel,
            firstStepVo: {},
            reqReportVo:{},
            inquiryModel: {
                id: '',
                cardType: 1,
                cardNum: '',
                userName: '',
                format: 2,
                note: '',
                phone:'',

            },
            report: {
                content: ''
            },

            step: 1,
            queryCount : 0,
            errMsg: {},
            manualFaceVerify: false,
            showError: function (reasons) {
                var result = {};
                for (var i = 0; i < reasons.length; i++) {
                    var reason = reasons[i];
                    var element = reason.element;
                    var name = element.id;
                    if (name) {
                        result[name] = reason;
                    }
                }
                vm.errMsg = result;
            },
            isIdCard: function () {
                return vm.inquiryModel.cardType == 1;
            },
           
            gotoStep: function (step) {
                vm.step=step;
            },
                       
           
            readIdCard: function () {
                var idCardData = Camera.readIdCardData();
                vm.inquiryModel.userName = idCardData.name;
                vm.inquiryModel.cardNum = idCardData.id;
                Camera.stopIdCard();
            },
            changeIdType: function(){
                vm.inquiryModel.cardType=$("#idType").val();
            },
            
            againPrintCreditReport: function () {
  	             	                  
                //branchquery/dissentInnerQueryActionV2.do?name=������&certtype=0&certno=412822198212021417&vertype=24
                //&dissentid=201706024401240400050_c5d7b54d717ec68e0e466fbc7a7adffb&queryreason=04&chargeNO=G2017060200000105705131
                var data = vm.reqReportVo.$model;
                data.name = vm.inquiryModel.userName;
                data.certno = vm.inquiryModel.cardNum;
                data.queryreason =  vm.inquiryModel.note;
                data.certtype = vm.inquiryModel.cardType;
 
                $.ajax({
                    url: 'inquiry/again/report',
                    type: 'post',
                    data: data,
                    dataType:"json",
                    success: function (resp) {
                        if (resp.code === 'SUCCESS') {
                            vm.gotoStep(6);
                            // vm.report.content = resp.data.html;
                            vm.inquiryModel.id = resp.data.id;
                            $('#reportContainer')[0].contentDocument.body.innerHTML = resp.data.html;
                            $('#reportContainer')[0].contentDocument.body.style.cssText = 'background-image:url(resources/images/pbccrc_watermark.gif);background-repeat:repeat;background-color: #ffffff;';
                            forbidLeftAndTop.forbid();
                        } else {
                        	
                        	if(resp.code == 'SID_CHECK_FAIL'){
      							location.href = resp.data;
      							return;
      						}
                        	
                        	var errorMsg = (resp.msg == "" ? "查询失败" : resp.msg);

                        	layer.msg(errorMsg);                        	
                        }
                    }
                });

          
            },
            printReport: function () {
                // window.print();
                // 这里需要弹框出来提示 完成 / 重新打印
                if (confirm("你确定要打印该报告吗？")) {
                    var data = {};

                    data.id = vm.inquiryModel.id;
                    data.printResult = 1;
                    $.ajax({
                        url: 'inquiry/credit/print',
                        type: 'post',
                        data: data,
                        dataType:"json",
                        success: function (resp) {
                            if (resp.code === 'SUCCESS') {
                                
                                // $('#reportContainer')[0].contentWindow.print();
                            }
                        }
                    });
                    
                    window.commonPrint('reportContainer');
                } else {
                    //alert("点击了取消");
                }
                

            },
            cancelPrintReport: function (flag) {
            	//打印结果(0：未打印，-1-取消打印， 1-成功，2-失败)
            	var printResult = -1;
            	var msg = "你确定要取消打印吗？";

                if (confirm(msg)) {
//                	if(printResult != 1){
//                		var data = {};
//
//                        data.id = vm.inquiryModel.id;
//                        data.printResult = printResult;
//                        $.ajax({
//                             url: 'inquiry/credit/print',
//                             type: 'post',
//                             data: data,
//                             dataType:"json",
//                             success: function (resp) {
//
//                             }
//                        });
//                	}
                	
                    forbidLeftAndTop.cancel();
                    // 跳转到初始界面
                    location.href="index?sid=" + sid + "&showMenu=PERSON_QUERY_A";
                    //$("#BUS_QUERY").click();$("#PERSON_QUERY_A").click();
                    // vm.gotoStep(1);
                }

            },
            submit: function () {
                // 提交数据并跳转到第四步
                var card = vm.inquiryModel.cardType;
            },
          
            removePic: function (index, type) {
                if (type == 2) {
                    vm.otherPic.splice(index, 1);
                } else {
                    vm.cardPic.splice(index, 1);
                }
            },
            validate: {
                onManual: $.noop,
                onValidateAll: function(reasons){
                	console.log('reasons:', reasons);
                    vm.showError(reasons);
                    if (!reasons.length) {
                        if (vm.isIdCard()) {
                            vm.gotoStep(2);
                        } else {
                            vm.gotoStep(3);
                        }
                    }
                },

                onError: function (reasons, ev) {
                    vm.showError(reasons);
                },
                onSuccess: function (reasons, ev) {
                    vm.errMsg = '';
                },

                validateAllInSubmit: false // {Boolean} true，在submit事件中执行onValidateAll回调

            }
        });
        
        avalon.scan($("#page-content-wrapper")[0]);
        window.setTimeout(function () {
            var cameraType = 4; // 资料拍摄
            if (vm.isIdCard()) {
                cameraType = 5;
            }
            Camera.destroy();
            Camera.init({
                container: '#videoContainer',
                id: 'ca',
                width:500,
                height:420,
                viewWidth:500,
                viewHeight:375,
                type: cameraType,
                facRect:true
            });

        }, 1000);
    });

}(window, jQuery, Camera));